# -*-makefile-*-
# @configure_input@
# Makefile.global.in - Makefile to be included by all submakes
#
# This file is converted by configure into an actual Makefile,
# replacing the @varname@ placeholders by actual values.
#
# This files is intended to contain infrastructure needed by several
# makefiles, particulary central handling of compilation flags and
# rules.

spq_abs_srcdir:=@abs_top_srcdir@/${spq_subdir}
spq_abs_top_srcdir:=@abs_top_srcdir@
postgres_abs_srcdir:=@POSTGRES_SRCDIR@
postgres_abs_builddir:=@POSTGRES_BUILDDIR@

PG_CONFIG:=@PG_CONFIG@
PGXS:=$(shell $(PG_CONFIG) --pgxs)

# if both, git is installed and there is a .git directory in the working dir we set the
# GIT_VERSION to a human readable gitref that resembles the version from which spq is
# built. During releases it will show the tagname which by convention is the verion of the
# release
ifneq (@GIT_BIN@,)
ifneq (@HAS_DOTGIT@,)
	# try to find a tag that exactly matches the current branch, swallow the error if cannot find such a tag
	GIT_VERSION := "$(shell @GIT_BIN@ describe --exact-match --dirty --always --tags 2>/dev/null)"

	# if there is not a tag that exactly matches the branch, then GIT_VERSION would still be empty
	# in that case, set GIT_VERSION with current branch's name and the short sha of the HEAD
ifeq ($(GIT_VERSION),"")
	GIT_VERSION := "$(shell @GIT_BIN@ rev-parse --abbrev-ref HEAD)(sha: $(shell @GIT_BIN@ rev-parse --short HEAD))"
endif
endif
endif

# Support for VPATH builds (i.e. builds from outside the source tree)
vpath_build=@vpath_build@
ifeq ($(vpath_build),yes)
	override VPATH:=$(spq_abs_srcdir)
	USE_VPATH:=$(VPATH)
	spq_top_srcdir:=$(spq_abs_top_srcdir)
	override srcdir=$(VPATH)
else
	spq_top_srcdir:=$(spq_top_builddir)
endif

# Citus is built using PostgreSQL's pgxs
USE_PGXS=1
include $(PGXS)

# Remake Makefile.global from Makefile.global.in if the latter
# changed. In order to trigger this rule, the including file must
# write `include $(spq_top_builddir)/Makefile.global', not some
# shortcut thereof.  This makes it less likely to accidentally run
# with some outdated Makefile.global.
# Make internally restarts whenever included Makefiles are
# regenerated.
$(spq_top_builddir)/Makefile.global: $(spq_abs_top_srcdir)/configure $(spq_abs_top_srcdir)/Makefile.global.in $(spq_top_builddir)/config.status
	cd @abs_top_builddir@ && ./config.status Makefile.global

# Ensure configuration is generated by the most recent configure,
# useful for longer existing build directories.
$(spq_top_builddir)/config.status: $(spq_abs_top_srcdir)/configure $(spq_abs_top_srcdir)/src/backend/distributed/spq.control
	cd @abs_top_builddir@ && ./config.status --recheck && ./config.status

# Regenerate configure if configure.ac changed
$(spq_abs_top_srcdir)/configure: $(spq_abs_top_srcdir)/configure.ac
	cd ${spq_abs_top_srcdir} && ./autogen.sh

# If specified via configure, replace the default compiler. Normally
# we'll build with the one postgres was built with. But it's useful to
# be able to use a different one, especially when building against
# distribution packages.
ifneq (@CXX@,)
    override CXX=@CXX@
endif

# If detected by our configure script, override the FLEX postgres
# detected.  That allows to compile spq against a postgres which was
# built without flex available (possible because generated files are
# included)
ifneq (@FLEX@,)
    override FLEX=@FLEX@
endif

override PGCFLAGS := $(shell $(PG_CONFIG) --cflags)
override OG_THIRD_BIN_PATH := $(shell echo ${THIRD_BIN_PATH})
override OG_CODE_BASE := $(shell echo ${CODE_BASE})
override THIRD_PARTY_INCLUDE:=-I '$(OG_THIRD_BIN_PATH)/kernel/dependency/libobs/comm/include'
override THIRD_PARTY_INCLUDE += -I '$(OG_CODE_BASE)/src/include'
override THIRD_PARTY_INCLUDE += -I '$(spq_abs_top_srcdir)/vendor/safestringlib/include'

# Add options passed to configure or computed therein, to CFLAGS/CPPFLAGS/...
override CXXFLAGS += @CXXFLAGS@ @SPQ_CXXFLAGS@ $(PGCFLAGS) -fPIC
# @TODO: temporary soltion, we'd better rename all .c file to .cc file
override CFLAGS := ${CXXFLAGS}
override BITCODE_CXXFLAGS := $(BITCODE_CXXFLAGS) @SPQ_BITCODE_CXXFLAGS@
ifneq ($(GIT_VERSION),)
    override CXXFLAGS += -DGIT_VERSION=\"$(GIT_VERSION)\"
endif

override CPPFLAGS := @CPPFLAGS@ @SPQ_CPPFLAGS@ -I '${spq_abs_top_srcdir}/src/include' -I'${spq_top_builddir}/src/include' $(CPPFLAGS) $(THIRD_PARTY_INCLUDE) -fPIC
override LDFLAGS += @LDFLAGS@ @SPQ_LDFLAGS@

# optional file with user defined, additional, rules
-include ${spq_abs_srcdir}/src/Makefile.custom
